package org.feiye.studyday19.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayList_NewsManger {

	public static void main(String[] args) {
		// 集合存储多条新闻标题
		NewsTitle title1 = new NewsTitle(1, "阿尔法狗大战李世石1", "admin1");
		NewsTitle title2 = new NewsTitle(2, "阿尔法狗大战李世石2", "admin2");
		NewsTitle title3 = new NewsTitle(3, "阿尔法狗大战李世石3", "admin3");
		NewsTitle title4 = new NewsTitle(4, "阿尔法狗大战李世石4", "admin4");
		NewsTitle title5 = new NewsTitle(5, "阿尔法狗大战李世石5", "admin5");
		NewsTitle title6 = new NewsTitle(6, "阿尔法狗大战李世石6", "admin6");

		List<NewsTitle> list = new ArrayList<NewsTitle>();
		// add方法：添加元素
		list.add(title1);
		list.add(title2);
		list.add(title3);
		list.add(title4);
		list.add(title5);
		list.add(title6);
		System.out.println("新闻标题总数：" + list.size());

		// 遍历list，取出每条新闻标题的题目
		for (int i = 0; i < list.size(); i++) {
			NewsTitle title = list.get(i);
			System.out.println(title.getTitle());
		}

		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

		// foreach遍历
		for (NewsTitle newsTitle : list) {
			System.out.println(newsTitle.getId() + "  " + newsTitle.getAuthor() + "  " + newsTitle.getTitle());
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		// Iterator遍历
		Iterator iterator = list.iterator();// 获得迭代器
		while (iterator.hasNext()) {
			NewsTitle title = (NewsTitle) iterator.next();
			System.out.println(title.getId() + "  " + title.getAuthor() + "  " + title.getTitle());
		}
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		// contains（方法）：是否包含该元素，remove（方法）：删除该元素
		System.out.println(list.contains(title6));
		list.remove(title6);
		System.out.println(list.contains(title6));
		System.out.println(list.size());
		// clear方法：清空集合元素
		list.clear();
		System.out.println(list.size());
		// isEmpty:判断集合是否为空
		boolean empty = list.isEmpty();
		System.out.println(empty);
	}

}
